import {getTaskContent, getTaskDetail, taskZan} from "../../api/activity";

import Util from '../../utils/util.js';

const app = getApp();
Page({
  data: {
    loading: false, // 是否加载中
    loadend: false, // 是否加载完毕
    loadTitle: '加载更多内容', // 提示语
    lists: [],
    page: 1,
    et_show: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;

    this.setData({
      task_id: options.id || ""
    })

    if (Util.isEmpty(this.data.task_id)) {
      return Util.Tips({title: "参数错误"}, {tab: 1, url: "../index/index"})
    }

    Util.chekWxLogin().then(res => {
      let userInfo = res.userInfo || null;
      that.setData({
        userInfo: userInfo
      })

      if (Util.isEmpty(userInfo.username) || Util.isEmpty(userInfo.avatar)) {
        return Util.Tips({title: "请先完善资料",}, {tab: 2, url: "../member/edit"})
      }

      this.initData();
    })

    this.onLoadTabbar();

  },

  initData(e) {
    const that = this;
    wx.showLoading({title: "正在加载..."})
    getTaskDetail({taskId: this.data.task_id}).then(res => {
      let task = res.data.task || null
      task.content = Util.formatRichText(task.content)
      that.setData({
        task: task,
        join: res.data.join || null,
      })
      that.setData({
        loading: false,
        loadend: false,
        loadTitle: '加载更多内容',
        lists: [],
        page: 1,
      })
      that.getLists();
      wx.hideLoading()
    }).catch(errmsg => {
      Util.Tips({title: errmsg})
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const is_refresh = wx.getStorageSync('is_refresh')
    if (is_refresh) {
      wx.removeStorageSync('is_refresh')
      this.initData();
    }
  },

  wfZan(e) {
    const that = this,
      key = e.currentTarget.dataset.k

    let lists = this.data.lists || null
    if (Util.isEmpty(lists))
      return;

    taskZan(lists[key].id).then(res => {
      if (res.data.status == 1) {
        lists[key]['is_zan'] = 1;
        lists[key]['star_amount'] = lists[key]['star_amount'] + 1;
      } else {
        lists[key]['is_zan'] = 0;
        lists[key]['star_amount'] = lists[key]['star_amount'] - 1;
      }

      that.setData({lists: lists})
    })

  },

  getLists() {
    const that = this
    if (that.data.loadend || that.data.loading) {
      return;
    }
    let filter = that.data.filter || {}
    filter['page_num'] = that.data.page;
    filter['taskId'] = that.data.task_id;

    that.setData({loading: true, loadTitle: '正在加载...'});
    let lists = that.data.lists || []
    getTaskContent(filter).then(res => {
      const list = res.data.items || []
      const loadend = res.data.last_page <= that.data.page;

      lists = app.SplitArray(list, lists);
      that.setData({
        lists: lists,
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '正在加载...',
        total: res.data.total,
        page: that.data.page + 1,
        et_show: lists.length > 0
      });
      wx.stopPullDownRefresh()
    }).catch(errmsg => {
      wx.stopPullDownRefresh()
      app.Tips({title: errmsg})
    })
  },

  previewMutiImage(e) {

    const that = this,
      index = e.currentTarget.dataset.index,
      key = e.currentTarget.dataset.key,
      field = e.currentTarget.dataset.field,
      lists = this.data.lists || null

    if (Util.isEmpty(lists) || Util.isEmpty(lists[index].extends[field].values))
      return
    let urls = []
    for (const k in lists[index].extends[field].values) {
      urls.push(Util.previewCdnImage(lists[index].extends[field].values[k].path));
    }

    if (Util.isEmpty(urls))
      return;

    wx.previewImage({
      current: Util.previewCdnImage(lists[index].extends[field].values[key].path),
      urls: urls
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      loading: false,
      loadend: false,
      loadTitle: '加载更多内容',
      lists: [],
      page: 1,
    })
    this.getLists();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getLists();
  },

  onLoadTabbar: function () {//底部tabbar返回数据
    const that = this;
    Util.checkIphone().then(res => {//检查是否是iphone x/11等
      that.setData({
        isIphoneX: res
      })
    });
    wx.createSelectorQuery().in(that).select('#weui-footer')
      .boundingClientRect(function (rect) {
        that.setData({
          bottom: parseInt(rect.height) + 15
        })
      }).exec()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})